package com.lmm.lcuser.lc_user.service;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;

import com.lmm.dresswisdom.lmmframe.retrofit.AppCallBack;
import com.lmm.dresswisdom.lmmframe.retrofit.AppClient;
import com.lmm.dresswisdom.lmmframe.util.LogUtils;
import com.lmm.lcuser.lc_user.BaseReturnInfo;
import com.lmm.lcuser.lc_user.commonbean.GoodsCategoryModel;
import com.lmm.lcuser.lc_user.commonbean.LogCategoryBean;
import com.lmm.lcuser.lc_user.config.AppInfoAction;
import com.lmm.lcuser.lc_user.config.ConfigValue;
import com.lmm.lcuser.lc_user.config.DataSafeUtil;
import com.lmm.lcuser.lc_user.dbmodel.GoodsCategoryRealm;
import com.lmm.lcuser.lc_user.dbmodel.LogCategoryRealm;
import com.lmm.lcuser.lc_user.utils.RealmUtils;

import java.util.List;
import java.util.Map;

import io.realm.Realm;

/**
 * @author xmgong
 * @date 2017/12/1
 * 后台更新服务
 */
public class UpdateIntentService extends IntentService {
    private final String TAG = "UpdateIntentService";
    private UpdateAction mAction;

    public UpdateIntentService() {
        super("UpdateIntentService");
        mAction = AppClient.getRetrofit(ConfigValue.apiUrl).create(UpdateAction.class);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        updateCategoryAction();
    }

    /**
     * 更新栏目
     */
    private void updateCategoryAction() {
        Map<String, String> params = AppInfoAction.getParams();
        AppClient.request(mAction.getGoodsCategoryAction(DataSafeUtil.EncodeMAP(params)), new AppCallBack<BaseReturnInfo<List<GoodsCategoryModel>>>() {
            @Override
            public void onSuccess(BaseReturnInfo<List<GoodsCategoryModel>> returnInfo) {
                if (returnInfo.isSuccess()) {

                    for (GoodsCategoryModel g : returnInfo.getData()) {
                        LogUtils.write(g.toString());
                    }

                    saveCategory(returnInfo.getData());
                }
            }

            @Override
            public void onFailure(String s) {
                LogUtils.write(s);
            }

            @Override
            public void onFinish() {
                updateLogCategoryAction();
            }
        });
    }

    /**
     * 更新日志数据
     */
    private void updateLogCategoryAction() {
        Map<String, String> params = AppInfoAction.getParams();
        AppClient.request(mAction.getLogcategoryAction(DataSafeUtil.EncodeMAP(params)), new AppCallBack<BaseReturnInfo<List<LogCategoryBean>>>() {
            @Override
            public void onSuccess(BaseReturnInfo<List<LogCategoryBean>> returnInfo) {
                if (returnInfo.isSuccess()) {
                    for (LogCategoryBean logCategoryBean : returnInfo.getData()) {
                        LogUtils.write(logCategoryBean.getCatName());
                    }
                    saveLogCategory(returnInfo.getData());
                }
            }

            @Override
            public void onFailure(String s) {

            }

            @Override
            public void onFinish() {

            }
        });
    }


    private void saveLogCategory(List<LogCategoryBean> logCategoryBeanList) {
        Realm realm = RealmUtils.getRealm();
        realm.beginTransaction();
        realm.delete(LogCategoryRealm.class);
        LogCategoryRealm item = null;
        for (LogCategoryBean key : logCategoryBeanList) {
            item = realm.createObject(LogCategoryRealm.class);
            item.setCatCode(key.getCatCode());
            item.setCatName(key.getCatName());
            item.setDefaultContent(key.getDefaultContent());
            item.setIcon(key.getIcon());
            item.setId(key.getId());
            item.setParentId(key.getParentId());
            realm.copyFromRealm(item);
        }
        realm.commitTransaction();
    }


    /**
     * 保存栏目
     *
     * @param categoryRealmList
     */
    private void saveCategory(List<GoodsCategoryModel> categoryRealmList) {
        Realm realm = RealmUtils.getRealm();
        realm.beginTransaction();
        realm.delete(GoodsCategoryRealm.class);
        GoodsCategoryRealm item = null;
        for (GoodsCategoryModel gcgm : categoryRealmList) {
            item = realm.createObject(GoodsCategoryRealm.class);
            item.setCatCode(gcgm.getCatCode());
            item.setCatId(gcgm.getCatId());
            item.setCatIndex(gcgm.getCatIndex());
            item.setCatName(gcgm.getCatName());
            item.setEnable(gcgm.getEnable());
            item.setSort(gcgm.getSort());
            item.setIsType(gcgm.getIsType());
            item.setParentId(gcgm.getParentId());
            realm.copyFromRealm(item);
        }
        realm.commitTransaction();
    }

}
